package cn.tedu.csmall.product.config;

import cn.tedu.csmall.product.interceptor.InsertUpdateTimeInterceptor;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * MyBatis配置类
 *
 * @author Lenovo
 */
@Slf4j
@Configuration
@MapperScan("cn.tedu.csmall.product.mapper")
public class MyBatisConfiguration {

    private final List<SqlSessionFactory> sqlSessionFactoryList;

    @Autowired
    public MyBatisConfiguration(List<SqlSessionFactory> sqlSessionFactoryList) {
        log.debug("创建配置类对象：MyBatisConfiguration");
        this.sqlSessionFactoryList = sqlSessionFactoryList;
    }

    /**
     * 为所有的SqlSessionFactory添加拦截器
     */
    @PostConstruct
    public void addInterceptor() {
        InsertUpdateTimeInterceptor interceptor = new InsertUpdateTimeInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        }
    }

}
